﻿/***************************************************************************
 * 文 件 名:   CMD_ChangeCamTarget
 * 
 * 描    述:   切换Target的指令
 * 
 * 版    本：  V1.1
 * 创 建 者：  京产肠饭、谭潇杰
 * 创建时间：  2023/5/9
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
****************************************************************************/

using DG.Tweening;
using QFramework;
using UnityEngine;

namespace HKTools
{
    public class CMD_ChangeCamTarget : AbstractCommand
    {
        // 延时切换时间
        float delayTime;

        HK_TargetEnum targetType;

        float moveSpeed;
        float zoomSpeed;

        // 切换摄像机目标时，是否闪现过去
        bool isFlash = false;
        bool isUseBlinkEffect = false;

        // 切换后项目是否可操控
        bool isLockCam;

        /// <summary>
        /// 根据 HK_TargetEnum 类型，切换到对应的Target
        /// </summary>
        /// <param name="_targetType">将要切换的目标</param>
        /// <param name="_moveSpeed">切换后相机移动的速度</param>
        /// <param name="_wheelSpeed">切换后相机缩放的速度</param>
        public CMD_ChangeCamTarget(HK_TargetEnum _targetType)
        {
            targetType = _targetType;
            isLockCam = false;
        }

        /// <summary>
        /// 设置 切换后相机移动的速度
        /// </summary>
        /// <param name="_moveSpeed">相机移动速度</param>
        /// <returns></returns>
        public CMD_ChangeCamTarget SetMoveSpeed(float _moveSpeed)
        {
            if (_moveSpeed < 0)
                _moveSpeed = 0;

            moveSpeed = _moveSpeed;
            return this;
        }

        /// <summary>
        /// 设置 切换后相机缩放的速度
        /// </summary>
        /// <param name="_zoomSpeed">相机缩放速度</param>
        /// <returns></returns>
        public CMD_ChangeCamTarget SetZoomSpeed(float _zoomSpeed)
        {
            if (_zoomSpeed < 0)
                _zoomSpeed = 0;

            zoomSpeed = _zoomSpeed;
            return this;
        }

        /// <summary>
        /// 设置 相机切换的延时时间
        /// </summary>
        /// <param name="_delayTime">延时时间</param>
        /// <returns></returns>
        public CMD_ChangeCamTarget SetDelayTime(float _delayTime)
        {
            delayTime = _delayTime;
            return this;
        }

        /// <summary>
        /// 设置 相机切换为闪现方式（取消切换过渡动画）
        /// </summary>
        /// <param name="_isUseBlinkEffect">是否启用眨眼效果</param>
        /// <returns></returns>
        public CMD_ChangeCamTarget SetUsingFlash(bool _isUseBlinkEffect = true)
        {
            isFlash = true;
            isUseBlinkEffect = _isUseBlinkEffect;
            return this;
        }

        /// <summary>
        /// 设置切换后，是否锁住相机
        /// </summary>
        /// <returns></returns>
        public CMD_ChangeCamTarget SetCamLock()
        {
            isLockCam = true;
            return this;
        }

        protected override void OnExecute()
        {
            IHK_TargetCamSystem targetCamSystem = this.GetSystem<IHK_TargetCamSystem>();

            if (targetCamSystem.CurTarget != null && targetCamSystem.CurTarget.targetType == targetType)
            {
                Debug.LogWarning("已经在当前目标，无需进行切换");
                return;
            }

            if (moveSpeed == 0)
                moveSpeed = targetCamSystem.DefaultMoveSpeed;

            if (zoomSpeed == 0)
                zoomSpeed = targetCamSystem.DefaultZoomSpeed;

            if (delayTime > 0)
            {
                DOTween.Sequence()
                    .AppendCallback(() => targetCamSystem.ChangeTarget(targetType, moveSpeed, zoomSpeed, isFlash, isUseBlinkEffect, isLockCam))
                    .SetDelay(delayTime);
            }
            else
            {
                if (delayTime < 0)
                {
                    Debug.LogError("相机切换的延时时间不能为负值！");
                }
                targetCamSystem.ChangeTarget(targetType, moveSpeed, zoomSpeed, isFlash, isUseBlinkEffect, isLockCam);
            }
        }
    }
}